
package com.ncu.week5_listview_l9;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import com.example.week5.R;
public class ViewActivity extends Activity {
    private ArrayList<ImageInfo> imageList;
    private int pos;
    private ImageView img_view;

    //手势管理器
    private GestureDetector getsture;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        getsture = new GestureDetector(ViewActivity.this, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                //右向左滑动
                if(e1.getX()-e2.getX()>100){
                    if(pos>0){
                        pos--;
                    }
                    if(pos<=0){
                        pos =0;
                        Toast.makeText(ViewActivity.this,"已经是第一张了",Toast.LENGTH_LONG).show();
                    }
                    String currentImgSrc = imageList.get(pos).getFile_path();
                    img_view.setImageBitmap(BitmapFactory.decodeFile(currentImgSrc));
                }
                //左向右滑动
                if(e1.getX()-e2.getX()<-100){
                    if(pos<imageList.size()){
                        pos++;
                    }
                    if(pos<=0){
                        pos =0;
                        Toast.makeText(ViewActivity.this,"已经是最后一张了",Toast.LENGTH_LONG).show();
                    }
                    String currentImgSrc = imageList.get(pos).getFile_path();
                    img_view.setImageBitmap(BitmapFactory.decodeFile(currentImgSrc));
                }
                return true;
            }
        });
        img_view = (ImageView)findViewById(R.id.img_view);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        pos = bundle.getInt("pos");
        imageList = (ArrayList<ImageInfo>)bundle.getSerializable("image_list");
        img_view.setImageBitmap(BitmapFactory.decodeFile(imageList.get(pos).getFile_path()));
    }
    @Override
    public boolean onTouchEvent(MotionEvent event){
        return getsture.onTouchEvent(event);
    }
}
